<?php
// Connect To DB
require_once("../../Build/engine/call.php");
// Functions
require_once("func.php");
require_once("readXML.php");

class smartic_elements{
	// Variables
	static $errors_c_num;	static $errors_v_num;	static $errors_t_num;	static $inform_optns;	private $valid_notes;
	// Construct
	public function __construct($debug_c_code=true,$javascript_v=true,$lang_c_path='en',$from_methods='POST'){
		// Global Settings For The Elements
		$this->javascript_v = $javascript_v;
		$this->debug_c_code = $debug_c_code;
		$this->from_methods = strtoupper($from_methods);
		// Read Language From The XML file
		$this->v_messages_s = XML::XML_reader($lang_c_path);
		$this->inform_optns = XML::XML_reader('smartic_elemnts_types');
		// Debuging
		if($debug_c_code === true || $debug_c_code !== false){
			// Debuging Global Settings
			$this->Debug(
				$this->v_messages_s->creation->not006,
				$this->v_messages_s->creation->not001,
				array(array($debug_c_code,$javascript_v),array(true,false),'V/V'),
				array($this->from_methods,array('POST','GET'),'V/V')
			);
		}
	}
	// Calculate How Many Errors
	private function error_calc($error_type,$id,$msg,$error_var){
		//error
		$this->errors[$error_type][$id][] = $msg;
		self::$$error_var++;
	}
	// Function For Debuging The Creation
	private function Debug($id,$msg){
		$rules = array_delete(func_get_args(),array('0','1'));
		foreach($rules as $rule){
			$Comparative = $rule[0]; $ComparativeTo = $rule[1]; $CompWHtoWh = explode('/',$rule[2]);
			if(is_array($Comparative)){
				foreach($Comparative as $key=>$val){
					$check = ($CompWHtoWh[0] == 'K') ? $key : $val;
					if((is_array($ComparativeTo) && ($CompWHtoWh[1] == 'V' && !in_array($check,$ComparativeTo,true)) || ($CompWHtoWh[1] == 'K' && !array_key_exists($check,$ComparativeTo))) || (!is_array($ComparativeTo) && $ComparativeTo != $check)){
						$this->error_calc('creationErrors',$id,sprintf($msg,$check),'errors_c_num');
					}
				}
			}else{
				if((is_array($ComparativeTo) && ($CompWHtoWh[1] == 'V' && !in_array($Comparative,$ComparativeTo,true)) || ($CompWHtoWh[1] == 'K' && !array_key_exists($Comparative,$ComparativeTo))) || (!is_array($ComparativeTo) && $ComparativeTo != $Comparative)){
					$this->error_calc('creationErrors',$id,sprintf($msg,$Comparative),'errors_c_num');
				}
			}
		}
	}
	// Sets New Element Name & Type
	public function sets($type,$name){
		//Debuging Name & Type
		if($this->debug_c_code === true || $this->debug_c_code !== false){
			// Name Validation
			if(!preg_match('/^_?[A-Za-z]+[A-Za-z0-9]+$/',$name)){
				$this->error_calc('creationErrors',$name,sprintf($this->v_messages_s->creation->not000,$name),'errors_c_num');
			}else if(isset($this->elements) && array_key_exists($name,$this->elements)){
				$this->error_calc('creationErrors',$name,sprintf($this->v_messages_s->creation->not002,$name),'errors_c_num');
			}
			// Type Validation
			$elem_info = get_object_vars($this->inform_optns);
			$this->Debug(
				$name,
				sprintf($this->v_messages_s->creation->not003,$name),
				array($type,$elem_info,'K/K')
			);
		}
		// Last Created Element
		$this->LaElem = $name;
		// Add Element Type To Elements Array
		$this->elements[$name] = array('type'=>$type);
		return $this;
	}
	// Function For Options
	private function options($options){
		switch($options[0]){
			case 'length':
				$MIN = ereg('^[0-9]+$',$options[1]) ? $options[1] : '';
				$MAX = ereg('^[0-9]+$',$options[2]) ? $options[2] : '';
				$optionA = array('min-length'=>$MIN,'max-length'=>$MAX);
			break;
			case 'list':
				switch($options[1]){
					case 'mysql':
						if(!empty($options[2]) || !empty($options[3]) || !empty($options[4])){
							$condeti = isset($options[5]) ? $options[5] : '';
							$mysql_i = array('K'=>$options[2],'T'=>$options[3],'Q'=>$options[4],'C'=>$condeti);
							$optionA = mysql2array($mysql_i);
						}
					break;
					case 'array':
						$optionA = $this->elements[$this->LaElem]['list'] = $options[2];
					break;
				}
			break;
			case 'label':
				$optionA = '<label for="'.$this->LaElem.'">'.$options[1].':</label>';
			break;
			case 'line-break':
				$optionA = '<br />';
			break;
			case 'multi-selection':
				$optionA = 'multiple="multiple"';
			break;
			default:
				$optionA = isset($options[1]) ? $options[1] : '';
			break;
		}
		return $optionA;
	}
	// Function To Set New Options
	public function setOption($option){
		$this->elements[$this->LaElem][$option] = $this->options(func_get_args());
		return $this;
	}
	// Publish The Code
	public function publishIt(){
		$elemOpt = $this->elements[$this->LaElem];
		if($this->debug_c_code === true || $this->debug_c_code !== false){
			if(empty($this->errors['creationErrors'][$this->LaElem])){
				// Element Options
				$options = get_object_vars($this->inform_optns->$elemOpt['type']);
				$options = $options['option'];
				// Debug Element Options
				$this->Debug(
					$this->LaElem,
					$this->v_messages_s->creation->not004,
					array($elemOpt,$options,'K/V')
				);
				//for php 5 Undefined error
				foreach($options as $v){
					if(empty($elemOpt[$v])){
						$elemOpt[$v] = '';
					}
				}
			}
			if(!empty($this->errors['creationErrors'][$this->LaElem])){
				// Make Function for errors
				$this->elements[$this->LaElem]['field'] = 'Make Function for errors';
				return false;
			}
		}
		
		//here the validation prosess
		/*$GLOBALS['errors']->validErrors = '';
		
		print_r($this->elements[$this->LaElem]);
		$METHOD = '_'.$this->METHOD;
		
		$value = $METHOD[$this->LaElem];
		$dvalue = ($value == '' || $value == NULL) ? $elemOpt['default-value'] : $value;
		if(!is_array($value)){
			$dvalue = htmlentities($dvalue,ENT_QUOTES,'UTF-8');
			$value = trim(html_entity_decode($value,ENT_QUOTES,'UTF-8'));
		}
		$this->getValues[$this->LaElem] = $dvalue;*/
		
		//print_r($this->inform_optns->$elemOpt['type']->option->attributes());
		
		//HTML element
		$HTML =& $this->elements[$this->LaElem]['field'];
		switch($elemOpt['type']){
			case 'text':
				$HTML = '<input type="text" id="'.$this->LaElem.'" name="'.$this->LaElem.'" value="'.$elemOpt['default-value'].'" maxlength="'.$elemOpt['length']['max-length'].'" '.$elemOpt['param'].' />';
			break;
			case 'password':
				$HTML = '<input type="password" id="'.$this->LaElem.'" name="'.$this->LaElem.'" value="'.$elemOpt['default-value'].'" maxlength="'.$elemOpt['length']['max-length'].'" '.$elemOpt['param'].' />';
			break;
			case 'textarea':
				$HTML = '<textarea id="'.$this->LaElem.'" name="'.$this->LaElem.'" '.$elemOpt['param'].'>'.$elemOpt['default-value'].'</textarea>';
			break;
			case 'radio':
				$tags = '<label for="'.$this->LaElem.'%1$d"><input type="radio" name="'.$this->LaElem.'" id="'.$this->LaElem.'%1$d" value="%2$s" %4$s '.$elemOpt['param'].' /> %3$s</label>';
				$tags .= $elemOpt['line-break'];
				$HTML = forTags($elemOpt['list'],$tags,$elemOpt['default-value'],'checked="checked"');
			break;
			case 'checkbox':
				$NAME = ($elemOpt['multiS'] == true) ? $this->LaElem.'[]' : $this->LaElem;
				$tags = '<label for="'.$this->LaElem.'%1$d">%3$s <input type="checkbox" name="'.$NAME.'" value="%2$s" id="'.$this->LaElem.'%1$d" %4$s '.$elemOpt['param'].' /></label>';
				$tags .= $elemOpt['line-break'];
				$HTML = forTags($elemOpt['list'],$tags,$elemOpt['default-value'],'checked="checked"');
			break;
			case 'select':
				$NAME = ($elemOpt['multi-selection'] == true) ? $this->LaElem.'[]' : $this->LaElem;
				$HTML = '<select name="'.$NAME.'" id="'.$this->LaElem.'" '.$elemOpt['param'].' '.$elemOpt['multi-selection'].'>';
				$HTML .= '<option value="">---&lt; Select &gt;---</option>';
				$tags = '<option value="%2$s" %4$s >%3$s</option>';
				$HTML .= forTags($elemOpt['list'],$tags,$elemOpt['default-value'],'selected="selected"');
				$HTML .= '</select>';
			break;
		}
		//javascript function
	}
	//to render HTML elements
	public function render($name,$type){
		if(!isset($this->elements[$name]) || !isset($this->elements[$name][$type])){
			echo sprintf($this->v_messages_s->creation->not005,$name);
			return false;
		}
		$elemOpt = $this->elements[$name][$type];
		switch($type){
			case 'label':
				//HTML Label
				//$redDot = ($elemOpt['required'] == true) ? '<span style="color:red;">*</span>' : '&nbsp;&nbsp;';
				echo $elemOpt;
			break;
			case 'field':
				echo $elemOpt;
				/*if(isset($this->errors[$name])){
					$errorMSG = '<div class="error" id="'.$name.'error">%s</div>';
					$messages = sprintf($errorMSG,$this->errors[$name]);
					echo $this->elements[$name]['field'].$messages;
				}*/
			break;
		}
	}
}
?>